<template>
    <view>
        <!-- pages/center/phone.wxml -->
        <header name="手机号修改" :type="1"></header>
        <text class="titles">请完成验证后更改</text>
        <view class="msg">
            <text>请输入{{ inputdata.phone }}收到的短信验证码</text>
            <view>
                <input type="text" placeholder="请输入短信验证码" @input="onInput" data-name="code" />
                <text>{{ codetxt }}</text>
            </view>
        </view>
        <view class="btns" @tap="submit">下一步</view>
    </view>
</template>

<script>
import header from '../../../components/header/headerBar.vue';
// pages/center/phone.js
const app = getApp();
var inittime = 120;
export default {
    components: {
        header
    },
    data() {
        return {
            inputdata: {
                phone: ''
            },

            isclick: true,
            codetxt: '获取验证码',
            iscode: 0,
            iseye: 0,
            isipx: ''
        };
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        this.inputdata.phone = options.phone;
        that.setData({
            isipx: app.globalData.statusHeight,
            inputdata: this.inputdata
        });
        this.getcode();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        onInput(e) {
            var dom = e.target.dataset.name;
            var data = this.inputdata;
            data[dom] = e.detail.value;
            this.setData({
                inputdata: data
            });
        },

        getcode: function () {
            var that = this;
            var phone = that.inputdata.phone;

            if (that.isclick) {
                if (!phone) {
                    uni.showToast({
                        title: '请输入手机号码',
                        icon: 'none'
                    });
                    return false;
                }

                var reg = /^1[3|5|8]\d{9}$/;
                var phones = /^0\d{2,3}|\d{7,8}$/;
                if (!reg.test(phone) && !phones.test(phone)) {
                    uni.showToast({
                        title: '请输入正确的手机号',
                        icon: 'none'
                    });
                    return false;
                }

                app.globalData
                    .http()
                    .getcode({
                        phone: phone,
                        type: 4
                    })
                    .then((res) => {
                        uni.showToast({
                            title: res.msg,
                            icon: 'none'
                        });

                        if (res.status) {
                            function daoshu() {
                                setTimeout(function () {
                                    inittime--;

                                    if (inittime > 0) {
                                        that.setData({
                                            codetxt: '(' + inittime + ')s',
                                            isclick: ''
                                        });
                                        daoshu();
                                    } else {
                                        that.setData({
                                            codetxt: '获取验证码',
                                            isclick: true
                                        });
                                        inittime = 120;
                                    }
                                }, 1000);
                            }

                            daoshu();
                        }
                    })
                    .catch((res) => {});
            }
        },

        submit() {
            var _this = this;

            var datas = {
                code: '请输入验证码'
            };

            for (var obj in datas) {
                if (!this.inputdata[obj]) {
                    uni.showToast({
                        title: datas[obj],
                        icon: 'none'
                    });
                    return false;
                }
            }

            app.globalData
                .http()
                .upphone(this.inputdata)
                .then((res) => {
                    uni.showToast({
                        title: res.msg,
                        icon: 'none'
                    });

                    if (res.status) {
                        uni.navigateBack({
                            delta: 1
                        });
                    } else {
                    }
                })
                .catch((res) => {});
        }
    }
};
</script>
<style lang="scss"> @import 'main.scss';
@import './yzphone.scss';
</style>
